/**
 * Promise 简便处理函数
 * @param success 成功回调
 * @param error 失败回调
 * @param fina 都会调用
 */
Promise.prototype.w_then = function (success = () => {}, error = () => {}, fina = () => {}) {
  this.then((data) => {
    success(data)
  }).catch((err) => {
    error(err)
  }).finally(() => {
    fina()
  })
  return this
}

Promise.prototype.w_then_finally = function (success = () => {}, fina = () => {}) {
  this.then((data) => {
    success(data)
  }).catch((err) => {
    console.log('w_then_finally err = ', err)
  }).finally(() => {
    fina()
  })
  return this
}

Promise.prototype.w_finally = function (fina = () => {}) {
  this.then(() => {
  }).catch((err) => {
    console.log('w_then_finally err = ', err)
  }).finally(() => {
    fina()
  })
  return this
}

/**
 * 同步执行数组中的Promise，如果有错误终止执行，直接抛出
 * @param promises Promise 数组
 * @returns {Promise<any>}
 */
Promise.w_sync = function (promises) {
  return new Promise(async (resolve, reject) => {
    try {
      for (let i = 0; i < promises.length; i++) {
        const promise = promises[i]
        await promise
      }
      resolve()
    } catch (e) {
      reject(e)
    }
  })
}
